local lunce = fk.CreateSkill {

  name = "joy__lunce",

  tags = {  },

}
local selfmark = function (player,p,mark,value)
  p:setMark(mark, value)
  player:doNotify("SetPlayerMark", json.encode{
    p.id,
    mark,
    value
  })
end


lunce:addEffect(fk.RoundStart, {
  name = "joy__lunce",
  anim_type = "special",
  events = {fk.RoundStart,"fk.joy_lanhai"},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(lunce.name) and (event == fk.RoundStart or target == player)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local ch1 = "joy_lunce1"
    local ch2 = "joy_lunce2"
    local ch3 = "joy_lunce3"
    local chs = {ch1,ch2,ch3}
    local cs = {}
    for _, c in ipairs(chs) do
      for _, p in ipairs(room.alive_players) do
        if p:getMark(c) == 0 then
          table.insertIfNeed(cs,c)
          break
        end
      end
    end
    local choice = room:askForChoices(player,cs,1,1,lunce.name,"选择一个计策",true,true,chs)
    if #choice > 0 then
      local tars = {}
      for _, p in ipairs(room.alive_players) do
        if p:getMark(choice[1]) == 0 then
          table.insertIfNeed(tars,p.id)
        end
      end
      if tars == 0 then return end
      local tar = room:askForChoosePlayers(player,tars,1,1,"选择一名角色，为其施加 <br>"..Fk:translate(choice[1]).."："..Fk:translate(":"..choice[1]),lunce.name,false)
      if #tar > 0 then
        event:setCostData(self, {choice[1],tar[1]})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tar = room:getPlayerById(event:getCostData(self)[2])
    local ch = event:getCostData(self)[1]
    room:setPlayerMark(tar,ch,player.id)
    selfmark(player,tar,"@@"..ch,1)
    if tar == room.current then
      room:setBanner("joy_lunce-turn",1)
    end
  end,
})
--[[睿智北冰洋
lunce:addEffect("fk.joy_lanhai", {
  name = "joy__lunce",
  anim_type = "special",
  events = {fk.RoundStart,"fk.joy_lanhai"},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(lunce.name) and (event == fk.RoundStart or target == player)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local ch1 = "joy_lunce1"
    local ch2 = "joy_lunce2"
    local ch3 = "joy_lunce3"
    local chs = {ch1,ch2,ch3}
    local cs = {}
    for _, c in ipairs(chs) do
      for _, p in ipairs(room.alive_players) do
        if p:getMark(c) == 0 then
          table.insertIfNeed(cs,c)
          break
        end
      end
    end
    local choice = room:askForChoices(player,cs,1,1,lunce.name,"选择一个计策",true,true,chs)
    if #choice > 0 then
      local tars = {}
      for _, p in ipairs(room.alive_players) do
        if p:getMark(choice[1]) == 0 then
          table.insertIfNeed(tars,p.id)
        end
      end
      if tars == 0 then return end
      local tar = room:askForChoosePlayers(player,tars,1,1,"选择一名角色，为其施加 <br>"..Fk:translate(choice[1]).."："..Fk:translate(":"..choice[1]),lunce.name,false)
      if #tar > 0 then
        event:setCostData(self, {choice[1],tar[1]})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tar = room:getPlayerById(event:getCostData(self)[2])
    local ch = event:getCostData(self)[1]
    room:setPlayerMark(tar,ch,player.id)
    selfmark(player,tar,"@@"..ch,1)
    if tar == room.current then
      room:setBanner("joy_lunce-turn",1)
    end
  end,
})
--]]
lunce:addEffect(fk.EventPhaseStart, {
  refresh_events = {fk.EventPhaseStart,fk.TargetSpecified,fk.TurnEnd,fk.TurnStart,fk.BuryVictim,fk.Damage},
  can_refresh = function (self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player:getMark("joy_lunce1") ~= 0 and player.phase == Player.Play
    elseif event == fk.TargetSpecified then
      if target == player and player:getMark("joy_lunce2") ~= 0 and player.room.current and player.room.current == player  and data.card.trueName == "slash" and data.firstTarget then
        local room = player.room
        local to = room:getPlayerById(data.to)
        if not to.dead  then
          local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
          if use_event == nil then return false end
          local x = player:getMark("joylunce2_" .. data.card.trueName.."-turn")
          if x == 0 then
            room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
              local use = e.data
              if use.from == player.id and use.card.trueName == data.card.trueName then
                x = e.id
                room:setPlayerMark(player, "joylunce2_" .. data.card.trueName.."-turn", x)
                return true
              end
            end, Player.HistoryTurn)
          end
          return x == use_event.id
        end
      end
    elseif event == fk.TurnEnd then
      return target == player and (player:getMark("joy_lunce1") ~= 0 or player:getMark("joy_lunce2") ~= 0 or player:getMark("joy_lunce3") ~= 0)
    elseif event == fk.TurnStart then
      return target == player
    elseif event == fk.BuryVictim then
      return target == player
    elseif event == fk.Damage then
      return target == player and player:getMark("joy_lunce3") ~= 0 and player.phase == Player.Play and data.card and data.card.trueName == "slash"
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local tar = room:getPlayerById(player:getMark("joy_lunce1"))
      room:notifySkillInvoked(tar,"上策","offensive")
      tar:broadcastSkillInvoke(lunce.name)
      room:doIndicate(tar.id,{player.id})
      local use = room:askForUseCard(player,"slash","slash","上策：你可以使用一张无距离、次数限制的【杀】",true,{bypass_times = true,bypass_distances = true})
      if use then
        use.extraUse = true
        room:addPlayerMark(tar,"@joy_lanhai-round",1)
        room:setPlayerMark(player,"joy_lunce1_vic-turn",1)
        room:setBanner("joy_lunce_vic",1)
        room:useCard(use)
      end
    elseif event == fk.TargetSpecified then
      local tar = room:getPlayerById(player:getMark("joy_lunce2"))
      room:notifySkillInvoked(tar,"中策","control")
      tar:broadcastSkillInvoke(lunce.name)
      room:addPlayerMark(tar,"@joy_lanhai-round",1)
      room:setPlayerMark(player,"joy_lunce2_vic-turn",1)
      room:setBanner("joy_lunce_vic",1)
      if not player:isNude() then
        local cards = room:askForCardsChosen(tar,player,1,1,"he",lunce.name,"中策：选择 %src 的一张牌获得:"..player.id)
        if #cards > 0 then
          room:obtainCard(tar,cards,false,fk.ReasonPrey,tar.id,lunce.name)
        end
      end
    elseif event == fk.TurnEnd then
      if target:getMark("joy_lunce3") ~= 0 and target:getMark("joy_lunce3_novic-turn") == 0 then
          local tar = room:getPlayerById(player:getMark("joy_lunce3"))
          room:notifySkillInvoked(tar,"下策","support")
          tar:broadcastSkillInvoke(lunce.name)
          room:addPlayerMark(tar,"@joy_lanhai-round",1)
          room:setPlayerMark(player,"joy_lunce3_vic-turn",1)
          room:setBanner("joy_lunce_vic",1)
          if not player.dead then
            local cards = room:askForCard(tar,1,3,true,lunce.name,true,".","下策：你可将至多3张牌交给 %src 并令其回复1点体力:"..player.id)
            if #cards > 0 then
              room:obtainCard(player,cards,false,fk.ReasonGive,tar.id,lunce.name)
              if not player.dead and player:isWounded() then
                room:recover { num = 1, skillName = lunce.name, who = player, recoverBy = tar}
              end
            end
          end
      end
      for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
        if player:getMark(ce) ~= 0 then
          if player:getMark(ce.."_vic-turn") == 0 then
            room:setBanner("joy_lunce_novic",1)
          end
        end
        room:setPlayerMark(player,ce,0)
        room:setPlayerMark(player,"@@"..ce,0)
      end
      room:setBanner("joy_lunce-turn",1)
    elseif event == fk.TurnStart then
      room:setBanner("joy_lunce-turn",0)
      room:setBanner("joy_lunce_vic",0)
      room:setBanner("joy_lunce_novic",0)
      if (player:getMark("joy_lunce1") ~= 0 or player:getMark("joy_lunce2") ~= 0 or player:getMark("joy_lunce3") ~= 0) then
        room:setBanner("joy_lunce-turn",1)
      end
    elseif event == fk.BuryVictim then
      for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
          if player:getMark(ce.."_vic-turn")  ~= 0 then
            room:setBanner("joy_lunce_novic",1)
          end
      end
      for _, p in ipairs(room.alive_players) do
        for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
          if p:getMark(ce) == player.id then
            if p:getMark(ce.."_vic-turn") == 0 then
              room:setBanner("joy_lunce_novic",1)
            end
          end
        end
      end
    elseif event == fk.Damage then
      room:setPlayerMark(player,"joy_lunce3_novic-turn",1)
    end
  end,
})
lunce:addEffect(fk.TargetSpecified, {
  refresh_events = {fk.EventPhaseStart,fk.TargetSpecified,fk.TurnEnd,fk.TurnStart,fk.BuryVictim,fk.Damage},
  can_refresh = function (self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player:getMark("joy_lunce1") ~= 0 and player.phase == Player.Play
    elseif event == fk.TargetSpecified then
      if target == player and player:getMark("joy_lunce2") ~= 0 and player.room.current and player.room.current == player  and data.card.trueName == "slash" and data.firstTarget then
        local room = player.room
        local to = room:getPlayerById(data.to)
        if not to.dead  then
          local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
          if use_event == nil then return false end
          local x = player:getMark("joylunce2_" .. data.card.trueName.."-turn")
          if x == 0 then
            room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
              local use = e.data
              if use.from == player.id and use.card.trueName == data.card.trueName then
                x = e.id
                room:setPlayerMark(player, "joylunce2_" .. data.card.trueName.."-turn", x)
                return true
              end
            end, Player.HistoryTurn)
          end
          return x == use_event.id
        end
      end
    elseif event == fk.TurnEnd then
      return target == player and (player:getMark("joy_lunce1") ~= 0 or player:getMark("joy_lunce2") ~= 0 or player:getMark("joy_lunce3") ~= 0)
    elseif event == fk.TurnStart then
      return target == player
    elseif event == fk.BuryVictim then
      return target == player
    elseif event == fk.Damage then
      return target == player and player:getMark("joy_lunce3") ~= 0 and player.phase == Player.Play and data.card and data.card.trueName == "slash"
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local tar = room:getPlayerById(player:getMark("joy_lunce1"))
      room:notifySkillInvoked(tar,"上策","offensive")
      tar:broadcastSkillInvoke(lunce.name)
      room:doIndicate(tar.id,{player.id})
      local use = room:askForUseCard(player,"slash","slash","上策：你可以使用一张无距离、次数限制的【杀】",true,{bypass_times = true,bypass_distances = true})
      if use then
        use.extraUse = true
        room:addPlayerMark(tar,"@joy_lanhai-round",1)
        room:setPlayerMark(player,"joy_lunce1_vic-turn",1)
        room:setBanner("joy_lunce_vic",1)
        room:useCard(use)
      end
    elseif event == fk.TargetSpecified then
      local tar = room:getPlayerById(player:getMark("joy_lunce2"))
      room:notifySkillInvoked(tar,"中策","control")
      tar:broadcastSkillInvoke(lunce.name)
      room:addPlayerMark(tar,"@joy_lanhai-round",1)
      room:setPlayerMark(player,"joy_lunce2_vic-turn",1)
      room:setBanner("joy_lunce_vic",1)
      if not player:isNude() then
        local cards = room:askForCardsChosen(tar,player,1,1,"he",lunce.name,"中策：选择 %src 的一张牌获得:"..player.id)
        if #cards > 0 then
          room:obtainCard(tar,cards,false,fk.ReasonPrey,tar.id,lunce.name)
        end
      end
    elseif event == fk.TurnEnd then
      if target:getMark("joy_lunce3") ~= 0 and target:getMark("joy_lunce3_novic-turn") == 0 then
          local tar = room:getPlayerById(player:getMark("joy_lunce3"))
          room:notifySkillInvoked(tar,"下策","support")
          tar:broadcastSkillInvoke(lunce.name)
          room:addPlayerMark(tar,"@joy_lanhai-round",1)
          room:setPlayerMark(player,"joy_lunce3_vic-turn",1)
          room:setBanner("joy_lunce_vic",1)
          if not player.dead then
            local cards = room:askForCard(tar,1,3,true,lunce.name,true,".","下策：你可将至多3张牌交给 %src 并令其回复1点体力:"..player.id)
            if #cards > 0 then
              room:obtainCard(player,cards,false,fk.ReasonGive,tar.id,lunce.name)
              if not player.dead and player:isWounded() then
                room:recover { num = 1, skillName = lunce.name, who = player, recoverBy = tar}
              end
            end
          end
      end
      for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
        if player:getMark(ce) ~= 0 then
          if player:getMark(ce.."_vic-turn") == 0 then
            room:setBanner("joy_lunce_novic",1)
          end
        end
        room:setPlayerMark(player,ce,0)
        room:setPlayerMark(player,"@@"..ce,0)
      end
      room:setBanner("joy_lunce-turn",1)
    elseif event == fk.TurnStart then
      room:setBanner("joy_lunce-turn",0)
      room:setBanner("joy_lunce_vic",0)
      room:setBanner("joy_lunce_novic",0)
      if (player:getMark("joy_lunce1") ~= 0 or player:getMark("joy_lunce2") ~= 0 or player:getMark("joy_lunce3") ~= 0) then
        room:setBanner("joy_lunce-turn",1)
      end
    elseif event == fk.BuryVictim then
      for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
          if player:getMark(ce.."_vic-turn")  ~= 0 then
            room:setBanner("joy_lunce_novic",1)
          end
      end
      for _, p in ipairs(room.alive_players) do
        for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
          if p:getMark(ce) == player.id then
            if p:getMark(ce.."_vic-turn") == 0 then
              room:setBanner("joy_lunce_novic",1)
            end
          end
        end
      end
    elseif event == fk.Damage then
      room:setPlayerMark(player,"joy_lunce3_novic-turn",1)
    end
  end,
})
lunce:addEffect(fk.TurnEnd, {
  refresh_events = {fk.EventPhaseStart,fk.TargetSpecified,fk.TurnEnd,fk.TurnStart,fk.BuryVictim,fk.Damage},
  can_refresh = function (self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player:getMark("joy_lunce1") ~= 0 and player.phase == Player.Play
    elseif event == fk.TargetSpecified then
      if target == player and player:getMark("joy_lunce2") ~= 0 and player.room.current and player.room.current == player  and data.card.trueName == "slash" and data.firstTarget then
        local room = player.room
        local to = room:getPlayerById(data.to)
        if not to.dead  then
          local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
          if use_event == nil then return false end
          local x = player:getMark("joylunce2_" .. data.card.trueName.."-turn")
          if x == 0 then
            room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
              local use = e.data
              if use.from == player.id and use.card.trueName == data.card.trueName then
                x = e.id
                room:setPlayerMark(player, "joylunce2_" .. data.card.trueName.."-turn", x)
                return true
              end
            end, Player.HistoryTurn)
          end
          return x == use_event.id
        end
      end
    elseif event == fk.TurnEnd then
      return target == player and (player:getMark("joy_lunce1") ~= 0 or player:getMark("joy_lunce2") ~= 0 or player:getMark("joy_lunce3") ~= 0)
    elseif event == fk.TurnStart then
      return target == player
    elseif event == fk.BuryVictim then
      return target == player
    elseif event == fk.Damage then
      return target == player and player:getMark("joy_lunce3") ~= 0 and player.phase == Player.Play and data.card and data.card.trueName == "slash"
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local tar = room:getPlayerById(player:getMark("joy_lunce1"))
      room:notifySkillInvoked(tar,"上策","offensive")
      tar:broadcastSkillInvoke(lunce.name)
      room:doIndicate(tar.id,{player.id})
      local use = room:askForUseCard(player,"slash","slash","上策：你可以使用一张无距离、次数限制的【杀】",true,{bypass_times = true,bypass_distances = true})
      if use then
        use.extraUse = true
        room:addPlayerMark(tar,"@joy_lanhai-round",1)
        room:setPlayerMark(player,"joy_lunce1_vic-turn",1)
        room:setBanner("joy_lunce_vic",1)
        room:useCard(use)
      end
    elseif event == fk.TargetSpecified then
      local tar = room:getPlayerById(player:getMark("joy_lunce2"))
      room:notifySkillInvoked(tar,"中策","control")
      tar:broadcastSkillInvoke(lunce.name)
      room:addPlayerMark(tar,"@joy_lanhai-round",1)
      room:setPlayerMark(player,"joy_lunce2_vic-turn",1)
      room:setBanner("joy_lunce_vic",1)
      if not player:isNude() then
        local cards = room:askForCardsChosen(tar,player,1,1,"he",lunce.name,"中策：选择 %src 的一张牌获得:"..player.id)
        if #cards > 0 then
          room:obtainCard(tar,cards,false,fk.ReasonPrey,tar.id,lunce.name)
        end
      end
    elseif event == fk.TurnEnd then
      if target:getMark("joy_lunce3") ~= 0 and target:getMark("joy_lunce3_novic-turn") == 0 then
          local tar = room:getPlayerById(player:getMark("joy_lunce3"))
          room:notifySkillInvoked(tar,"下策","support")
          tar:broadcastSkillInvoke(lunce.name)
          room:addPlayerMark(tar,"@joy_lanhai-round",1)
          room:setPlayerMark(player,"joy_lunce3_vic-turn",1)
          room:setBanner("joy_lunce_vic",1)
          if not player.dead then
            local cards = room:askForCard(tar,1,3,true,lunce.name,true,".","下策：你可将至多3张牌交给 %src 并令其回复1点体力:"..player.id)
            if #cards > 0 then
              room:obtainCard(player,cards,false,fk.ReasonGive,tar.id,lunce.name)
              if not player.dead and player:isWounded() then
                room:recover { num = 1, skillName = lunce.name, who = player, recoverBy = tar}
              end
            end
          end
      end
      for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
        if player:getMark(ce) ~= 0 then
          if player:getMark(ce.."_vic-turn") == 0 then
            room:setBanner("joy_lunce_novic",1)
          end
        end
        room:setPlayerMark(player,ce,0)
        room:setPlayerMark(player,"@@"..ce,0)
      end
      room:setBanner("joy_lunce-turn",1)
    elseif event == fk.TurnStart then
      room:setBanner("joy_lunce-turn",0)
      room:setBanner("joy_lunce_vic",0)
      room:setBanner("joy_lunce_novic",0)
      if (player:getMark("joy_lunce1") ~= 0 or player:getMark("joy_lunce2") ~= 0 or player:getMark("joy_lunce3") ~= 0) then
        room:setBanner("joy_lunce-turn",1)
      end
    elseif event == fk.BuryVictim then
      for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
          if player:getMark(ce.."_vic-turn")  ~= 0 then
            room:setBanner("joy_lunce_novic",1)
          end
      end
      for _, p in ipairs(room.alive_players) do
        for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
          if p:getMark(ce) == player.id then
            if p:getMark(ce.."_vic-turn") == 0 then
              room:setBanner("joy_lunce_novic",1)
            end
          end
        end
      end
    elseif event == fk.Damage then
      room:setPlayerMark(player,"joy_lunce3_novic-turn",1)
    end
  end,
})
lunce:addEffect(fk.TurnStart, {
  refresh_events = {fk.EventPhaseStart,fk.TargetSpecified,fk.TurnEnd,fk.TurnStart,fk.BuryVictim,fk.Damage},
  can_refresh = function (self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player:getMark("joy_lunce1") ~= 0 and player.phase == Player.Play
    elseif event == fk.TargetSpecified then
      if target == player and player:getMark("joy_lunce2") ~= 0 and player.room.current and player.room.current == player  and data.card.trueName == "slash" and data.firstTarget then
        local room = player.room
        local to = room:getPlayerById(data.to)
        if not to.dead  then
          local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
          if use_event == nil then return false end
          local x = player:getMark("joylunce2_" .. data.card.trueName.."-turn")
          if x == 0 then
            room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
              local use = e.data
              if use.from == player.id and use.card.trueName == data.card.trueName then
                x = e.id
                room:setPlayerMark(player, "joylunce2_" .. data.card.trueName.."-turn", x)
                return true
              end
            end, Player.HistoryTurn)
          end
          return x == use_event.id
        end
      end
    elseif event == fk.TurnEnd then
      return target == player and (player:getMark("joy_lunce1") ~= 0 or player:getMark("joy_lunce2") ~= 0 or player:getMark("joy_lunce3") ~= 0)
    elseif event == fk.TurnStart then
      return target == player
    elseif event == fk.BuryVictim then
      return target == player
    elseif event == fk.Damage then
      return target == player and player:getMark("joy_lunce3") ~= 0 and player.phase == Player.Play and data.card and data.card.trueName == "slash"
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local tar = room:getPlayerById(player:getMark("joy_lunce1"))
      room:notifySkillInvoked(tar,"上策","offensive")
      tar:broadcastSkillInvoke(lunce.name)
      room:doIndicate(tar.id,{player.id})
      local use = room:askForUseCard(player,"slash","slash","上策：你可以使用一张无距离、次数限制的【杀】",true,{bypass_times = true,bypass_distances = true})
      if use then
        use.extraUse = true
        room:addPlayerMark(tar,"@joy_lanhai-round",1)
        room:setPlayerMark(player,"joy_lunce1_vic-turn",1)
        room:setBanner("joy_lunce_vic",1)
        room:useCard(use)
      end
    elseif event == fk.TargetSpecified then
      local tar = room:getPlayerById(player:getMark("joy_lunce2"))
      room:notifySkillInvoked(tar,"中策","control")
      tar:broadcastSkillInvoke(lunce.name)
      room:addPlayerMark(tar,"@joy_lanhai-round",1)
      room:setPlayerMark(player,"joy_lunce2_vic-turn",1)
      room:setBanner("joy_lunce_vic",1)
      if not player:isNude() then
        local cards = room:askForCardsChosen(tar,player,1,1,"he",lunce.name,"中策：选择 %src 的一张牌获得:"..player.id)
        if #cards > 0 then
          room:obtainCard(tar,cards,false,fk.ReasonPrey,tar.id,lunce.name)
        end
      end
    elseif event == fk.TurnEnd then
      if target:getMark("joy_lunce3") ~= 0 and target:getMark("joy_lunce3_novic-turn") == 0 then
          local tar = room:getPlayerById(player:getMark("joy_lunce3"))
          room:notifySkillInvoked(tar,"下策","support")
          tar:broadcastSkillInvoke(lunce.name)
          room:addPlayerMark(tar,"@joy_lanhai-round",1)
          room:setPlayerMark(player,"joy_lunce3_vic-turn",1)
          room:setBanner("joy_lunce_vic",1)
          if not player.dead then
            local cards = room:askForCard(tar,1,3,true,lunce.name,true,".","下策：你可将至多3张牌交给 %src 并令其回复1点体力:"..player.id)
            if #cards > 0 then
              room:obtainCard(player,cards,false,fk.ReasonGive,tar.id,lunce.name)
              if not player.dead and player:isWounded() then
                room:recover { num = 1, skillName = lunce.name, who = player, recoverBy = tar}
              end
            end
          end
      end
      for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
        if player:getMark(ce) ~= 0 then
          if player:getMark(ce.."_vic-turn") == 0 then
            room:setBanner("joy_lunce_novic",1)
          end
        end
        room:setPlayerMark(player,ce,0)
        room:setPlayerMark(player,"@@"..ce,0)
      end
      room:setBanner("joy_lunce-turn",1)
    elseif event == fk.TurnStart then
      room:setBanner("joy_lunce-turn",0)
      room:setBanner("joy_lunce_vic",0)
      room:setBanner("joy_lunce_novic",0)
      if (player:getMark("joy_lunce1") ~= 0 or player:getMark("joy_lunce2") ~= 0 or player:getMark("joy_lunce3") ~= 0) then
        room:setBanner("joy_lunce-turn",1)
      end
    elseif event == fk.BuryVictim then
      for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
          if player:getMark(ce.."_vic-turn")  ~= 0 then
            room:setBanner("joy_lunce_novic",1)
          end
      end
      for _, p in ipairs(room.alive_players) do
        for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
          if p:getMark(ce) == player.id then
            if p:getMark(ce.."_vic-turn") == 0 then
              room:setBanner("joy_lunce_novic",1)
            end
          end
        end
      end
    elseif event == fk.Damage then
      room:setPlayerMark(player,"joy_lunce3_novic-turn",1)
    end
  end,
})
lunce:addEffect(fk.BuryVictim, {
  refresh_events = {fk.EventPhaseStart,fk.TargetSpecified,fk.TurnEnd,fk.TurnStart,fk.BuryVictim,fk.Damage},
  can_refresh = function (self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player:getMark("joy_lunce1") ~= 0 and player.phase == Player.Play
    elseif event == fk.TargetSpecified then
      if target == player and player:getMark("joy_lunce2") ~= 0 and player.room.current and player.room.current == player  and data.card.trueName == "slash" and data.firstTarget then
        local room = player.room
        local to = room:getPlayerById(data.to)
        if not to.dead  then
          local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
          if use_event == nil then return false end
          local x = player:getMark("joylunce2_" .. data.card.trueName.."-turn")
          if x == 0 then
            room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
              local use = e.data
              if use.from == player.id and use.card.trueName == data.card.trueName then
                x = e.id
                room:setPlayerMark(player, "joylunce2_" .. data.card.trueName.."-turn", x)
                return true
              end
            end, Player.HistoryTurn)
          end
          return x == use_event.id
        end
      end
    elseif event == fk.TurnEnd then
      return target == player and (player:getMark("joy_lunce1") ~= 0 or player:getMark("joy_lunce2") ~= 0 or player:getMark("joy_lunce3") ~= 0)
    elseif event == fk.TurnStart then
      return target == player
    elseif event == fk.BuryVictim then
      return target == player
    elseif event == fk.Damage then
      return target == player and player:getMark("joy_lunce3") ~= 0 and player.phase == Player.Play and data.card and data.card.trueName == "slash"
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local tar = room:getPlayerById(player:getMark("joy_lunce1"))
      room:notifySkillInvoked(tar,"上策","offensive")
      tar:broadcastSkillInvoke(lunce.name)
      room:doIndicate(tar.id,{player.id})
      local use = room:askForUseCard(player,"slash","slash","上策：你可以使用一张无距离、次数限制的【杀】",true,{bypass_times = true,bypass_distances = true})
      if use then
        use.extraUse = true
        room:addPlayerMark(tar,"@joy_lanhai-round",1)
        room:setPlayerMark(player,"joy_lunce1_vic-turn",1)
        room:setBanner("joy_lunce_vic",1)
        room:useCard(use)
      end
    elseif event == fk.TargetSpecified then
      local tar = room:getPlayerById(player:getMark("joy_lunce2"))
      room:notifySkillInvoked(tar,"中策","control")
      tar:broadcastSkillInvoke(lunce.name)
      room:addPlayerMark(tar,"@joy_lanhai-round",1)
      room:setPlayerMark(player,"joy_lunce2_vic-turn",1)
      room:setBanner("joy_lunce_vic",1)
      if not player:isNude() then
        local cards = room:askForCardsChosen(tar,player,1,1,"he",lunce.name,"中策：选择 %src 的一张牌获得:"..player.id)
        if #cards > 0 then
          room:obtainCard(tar,cards,false,fk.ReasonPrey,tar.id,lunce.name)
        end
      end
    elseif event == fk.TurnEnd then
      if target:getMark("joy_lunce3") ~= 0 and target:getMark("joy_lunce3_novic-turn") == 0 then
          local tar = room:getPlayerById(player:getMark("joy_lunce3"))
          room:notifySkillInvoked(tar,"下策","support")
          tar:broadcastSkillInvoke(lunce.name)
          room:addPlayerMark(tar,"@joy_lanhai-round",1)
          room:setPlayerMark(player,"joy_lunce3_vic-turn",1)
          room:setBanner("joy_lunce_vic",1)
          if not player.dead then
            local cards = room:askForCard(tar,1,3,true,lunce.name,true,".","下策：你可将至多3张牌交给 %src 并令其回复1点体力:"..player.id)
            if #cards > 0 then
              room:obtainCard(player,cards,false,fk.ReasonGive,tar.id,lunce.name)
              if not player.dead and player:isWounded() then
                room:recover { num = 1, skillName = lunce.name, who = player, recoverBy = tar}
              end
            end
          end
      end
      for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
        if player:getMark(ce) ~= 0 then
          if player:getMark(ce.."_vic-turn") == 0 then
            room:setBanner("joy_lunce_novic",1)
          end
        end
        room:setPlayerMark(player,ce,0)
        room:setPlayerMark(player,"@@"..ce,0)
      end
      room:setBanner("joy_lunce-turn",1)
    elseif event == fk.TurnStart then
      room:setBanner("joy_lunce-turn",0)
      room:setBanner("joy_lunce_vic",0)
      room:setBanner("joy_lunce_novic",0)
      if (player:getMark("joy_lunce1") ~= 0 or player:getMark("joy_lunce2") ~= 0 or player:getMark("joy_lunce3") ~= 0) then
        room:setBanner("joy_lunce-turn",1)
      end
    elseif event == fk.BuryVictim then
      for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
          if player:getMark(ce.."_vic-turn")  ~= 0 then
            room:setBanner("joy_lunce_novic",1)
          end
      end
      for _, p in ipairs(room.alive_players) do
        for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
          if p:getMark(ce) == player.id then
            if p:getMark(ce.."_vic-turn") == 0 then
              room:setBanner("joy_lunce_novic",1)
            end
          end
        end
      end
    elseif event == fk.Damage then
      room:setPlayerMark(player,"joy_lunce3_novic-turn",1)
    end
  end,
})
lunce:addEffect(fk.Damage, {
  refresh_events = {fk.EventPhaseStart,fk.TargetSpecified,fk.TurnEnd,fk.TurnStart,fk.BuryVictim,fk.Damage},
  can_refresh = function (self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player:getMark("joy_lunce1") ~= 0 and player.phase == Player.Play
    elseif event == fk.TargetSpecified then
      if target == player and player:getMark("joy_lunce2") ~= 0 and player.room.current and player.room.current == player  and data.card.trueName == "slash" and data.firstTarget then
        local room = player.room
        local to = room:getPlayerById(data.to)
        if not to.dead  then
          local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
          if use_event == nil then return false end
          local x = player:getMark("joylunce2_" .. data.card.trueName.."-turn")
          if x == 0 then
            room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
              local use = e.data
              if use.from == player.id and use.card.trueName == data.card.trueName then
                x = e.id
                room:setPlayerMark(player, "joylunce2_" .. data.card.trueName.."-turn", x)
                return true
              end
            end, Player.HistoryTurn)
          end
          return x == use_event.id
        end
      end
    elseif event == fk.TurnEnd then
      return target == player and (player:getMark("joy_lunce1") ~= 0 or player:getMark("joy_lunce2") ~= 0 or player:getMark("joy_lunce3") ~= 0)
    elseif event == fk.TurnStart then
      return target == player
    elseif event == fk.BuryVictim then
      return target == player
    elseif event == fk.Damage then
      return target == player and player:getMark("joy_lunce3") ~= 0 and player.phase == Player.Play and data.card and data.card.trueName == "slash"
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local tar = room:getPlayerById(player:getMark("joy_lunce1"))
      room:notifySkillInvoked(tar,"上策","offensive")
      tar:broadcastSkillInvoke(lunce.name)
      room:doIndicate(tar.id,{player.id})
      local use = room:askForUseCard(player,"slash","slash","上策：你可以使用一张无距离、次数限制的【杀】",true,{bypass_times = true,bypass_distances = true})
      if use then
        use.extraUse = true
        room:addPlayerMark(tar,"@joy_lanhai-round",1)
        room:setPlayerMark(player,"joy_lunce1_vic-turn",1)
        room:setBanner("joy_lunce_vic",1)
        room:useCard(use)
      end
    elseif event == fk.TargetSpecified then
      local tar = room:getPlayerById(player:getMark("joy_lunce2"))
      room:notifySkillInvoked(tar,"中策","control")
      tar:broadcastSkillInvoke(lunce.name)
      room:addPlayerMark(tar,"@joy_lanhai-round",1)
      room:setPlayerMark(player,"joy_lunce2_vic-turn",1)
      room:setBanner("joy_lunce_vic",1)
      if not player:isNude() then
        local cards = room:askForCardsChosen(tar,player,1,1,"he",lunce.name,"中策：选择 %src 的一张牌获得:"..player.id)
        if #cards > 0 then
          room:obtainCard(tar,cards,false,fk.ReasonPrey,tar.id,lunce.name)
        end
      end
    elseif event == fk.TurnEnd then
      if target:getMark("joy_lunce3") ~= 0 and target:getMark("joy_lunce3_novic-turn") == 0 then
          local tar = room:getPlayerById(player:getMark("joy_lunce3"))
          room:notifySkillInvoked(tar,"下策","support")
          tar:broadcastSkillInvoke(lunce.name)
          room:addPlayerMark(tar,"@joy_lanhai-round",1)
          room:setPlayerMark(player,"joy_lunce3_vic-turn",1)
          room:setBanner("joy_lunce_vic",1)
          if not player.dead then
            local cards = room:askForCard(tar,1,3,true,lunce.name,true,".","下策：你可将至多3张牌交给 %src 并令其回复1点体力:"..player.id)
            if #cards > 0 then
              room:obtainCard(player,cards,false,fk.ReasonGive,tar.id,lunce.name)
              if not player.dead and player:isWounded() then
                room:recover { num = 1, skillName = lunce.name, who = player, recoverBy = tar}
              end
            end
          end
      end
      for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
        if player:getMark(ce) ~= 0 then
          if player:getMark(ce.."_vic-turn") == 0 then
            room:setBanner("joy_lunce_novic",1)
          end
        end
        room:setPlayerMark(player,ce,0)
        room:setPlayerMark(player,"@@"..ce,0)
      end
      room:setBanner("joy_lunce-turn",1)
    elseif event == fk.TurnStart then
      room:setBanner("joy_lunce-turn",0)
      room:setBanner("joy_lunce_vic",0)
      room:setBanner("joy_lunce_novic",0)
      if (player:getMark("joy_lunce1") ~= 0 or player:getMark("joy_lunce2") ~= 0 or player:getMark("joy_lunce3") ~= 0) then
        room:setBanner("joy_lunce-turn",1)
      end
    elseif event == fk.BuryVictim then
      for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
          if player:getMark(ce.."_vic-turn")  ~= 0 then
            room:setBanner("joy_lunce_novic",1)
          end
      end
      for _, p in ipairs(room.alive_players) do
        for _, ce in ipairs({"joy_lunce1","joy_lunce2","joy_lunce3"}) do
          if p:getMark(ce) == player.id then
            if p:getMark(ce.."_vic-turn") == 0 then
              room:setBanner("joy_lunce_novic",1)
            end
          end
        end
      end
    elseif event == fk.Damage then
      room:setPlayerMark(player,"joy_lunce3_novic-turn",1)
    end
  end,
})

return lunce